﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Threading;

namespace Cabal.Kernel.Output
{
    public class RequestItem
    {
        public RequestItem(string describe)
        {
            RequestThread = Thread.CurrentThread;
            Describe = describe;
            Headers = new WebHeaderCollection();
        }
        public RequestItem(BaseOutput target, string describe)
        {
            RequestThread = Thread.CurrentThread;
            Worker = target;
            Describe = describe;
            Headers = new WebHeaderCollection();
        }


        //Point
        public BaseOutput Worker;
        public Thread RequestThread;
        public Thread RunningThread;


        //Data
        public string RequestIP;

        public string Method = "GET";
        public string UploadFileNmae;
        public Uri TargetAddress;
        public WebHeaderCollection Headers;
        public byte[] RequestData;
        public DateTime RequestTime;

        public bool RequestDone = false;
        public WebHeaderCollection ResponseHeaders;
        public byte[] ResponseData;
        public DateTime ResopnseTime;


        //Flag
        public bool IsDone = false;
        public bool IsWakeUp = false;
        public bool IsError = false;
        public string Describe;
        public Exception Error;


        //Actions
        public void WakeUpRequestThread()
        {
            if (RequestThread.ThreadState != ThreadState.WaitSleepJoin)
                return;

            //IsWakeUp = true;
            if(IsWakeUp)
                RequestThread.Interrupt(); //调用线程必须接收
        }

        public override string ToString()
        {
            if(RunningThread!=null && RunningThread.ThreadState == ThreadState.Running)
                return "[Request][R][" + Method + "]" + Describe;
            else
                return "[Request][N][" + Method + "]" + Describe;
        }
    }
}
